<?php namespace App\Http\Controllers\Api\Admin;

use App\Goods;
use App\GoodsAttr;
use App\Http\Controllers\Controller;
use App\Http\Services\AdminManager;
use App\SysBanner;
use Illuminate\Http\Request;
use Auth;

class GoodsController extends Controller
{
    public function addGoods(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $name    = $request->get('name');
        $model   = $request->get('model');
        $type    = $request->get('type');
        $price   = $request->get('price');
        $stock   = $request->get('stock');
        $info    = $request->get('info');
        $thumbId = $request->get('thumb_id');
        $attrArr = $request->get('attr');
        $id      = $request->get('id');

        $thatGoods = Goods::whereId($id)->first();

        if(!$thatGoods){
            $thatGoods = new Goods();
        }

        if(!is_null($name))$thatGoods->name   = $name;
        if(!is_null($model))$thatGoods->model = $model;
        if(!is_null($type))$thatGoods->type   = $type;
        if($type == 1){
            if(!is_null($price))$thatGoods->coin = $price;
        }elseif($type == 2){
            if(!is_null($price))$thatGoods->amount = $price;
        }
        if(!is_null($stock))$thatGoods->stock      = $stock;
        if(!is_null($info))$thatGoods->info        = $info;
        if(!is_null($thumbId))$thatGoods->thumb_id = $thumbId;

        $thatGoods->save();

        if(is_array($attrArr) && count($attrArr) > 0){
            $thatAttrs = GoodsAttr::whereGoodId($thatGoods->id)->get();
            if(count($thatAttrs) > 0){
                foreach ($thatAttrs as $thatAttr){
                    $thatAttr->delete();
                }
            }

            foreach ($attrArr as $attr){
                $newAttr = new GoodsAttr();
                $newAttr->good_id = $thatGoods->id;
                $newAttr->name    = $attr;
                $newAttr->save();
            }
        }

        return response()->json(['level'=>'success','message'=>trans("添加或设置完成！"),'data'=>$thatGoods]);
    }

    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = SysBanner::whereId(trim($id))->first();

        if(!$thatBanner)
            $thatBanner = new SysBanner();

        if(!is_null($title))$thatBanner->title = $title;
        if(!is_null($linkUrl))$thatBanner->link_url = $linkUrl;
        if(!is_null($sort))$thatBanner->sort = $sort;
        if(!is_null($imgId))$thatBanner->img_id = $imgId;
        $thatBanner->save();

        return response()->json(['level'=>'success','message'=>trans("添加或修改成功"),'data'=>$thatBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var SysBanner $thatBanner */
        $thatBanner = SysBanner::whereId($id)->first();

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"],203);

        $thatBanner->delete();

        return response()->json(['level'=>'success','message'=>trans("删除成功"),'data'=>$thatBanner]);
    }

    public function setBannerStatus(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！'], 203);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var SysBanner $thatBanner */
        $thatBanner = SysBanner::whereId($id)->first();
        if(!$thatBanner)
            return response()->json(['level'=>"error",'message'=>"该banner不存在！"],203);

        if($thatBanner->status == 0)
            $thatBanner->status = 1;
        else
            $thatBanner->status = 0;

        $thatBanner->save();

        return response()->json(['level'=>'success','message'=>trans("设置banner状态成功!"),'data'=>$thatBanner]);
    }
}